<?php

use Biz\cache\brand\BrandShowActivity;
use Biz\model\activity\NarwalDesigner as mNarwalDesigner;
use Doraemon\service\User;

/**
 * @deprecated
 * 云鲸扫地机器人设计师征集令
 * Class SNJRecordAction
 */
class NarwalDesignerAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const VALIDATOR_TOAST_DEBUG = true;
    const VALIDATOR_RULES = [
        'water_position' => 'require',
        'install_position' => 'require',
        'connect_water' => 'require',
        'more_beautiful' => 'require',
        'house_suggest' => 'require',
        'design_img' => 'require',
        'elevation_img' => 'require',
    ];
    const VALIDATOR_MSG = [
        'water_position.require' => '请选择水源位置',
        'install_position.require' => '请选择云鲸产品的安装位置',
        'connect_water.require' => '请具体说明您的上下水连接方式及管线排布',
        'more_beautiful.require' => '请具体描述云鲸怎样和室内设计更好的融合',
        'house_suggest.require' => '请至少选择一项适用家庭建议',
        'design_img.require' => '请上传效果图',
        'elevation_img.require' => '请上传立面图',
    ];

    public function main()
    {
        $data = [];
        $data['water_position'] = $this->getRequest()->getPost('water_position', '');//水源位置
        $data['install_position'] = $this->getRequest()->getPost('install_position', '');//安装位置
        $data['connect_water'] = $this->getRequest()->getPost('connect_water', '');//如何连接上下水
        $data['more_beautiful'] = $this->getRequest()->getPost('more_beautiful', '');//如何提升美观性
        $data['house_suggest'] = $this->getRequest()->getPost('house_suggest', ''); // 适用家庭建议（英文逗号隔开）
        $data['design_img'] = $this->getRequest()->getPost('design_img', '');//效果图
        $data['elevation_img'] = $this->getRequest()->getPost('elevation_img', '');//立面图
        $data['name'] = $this->getRequest()->getPost('name', '');//姓名
        $data['phone'] = $this->getRequest()->getPost('phone', '');//手机号
        $data['area'] = $this->getRequest()->getPost('area', '');//所在城市
        $lockKey = 'narwaldesigner';
        if (empty($this->uid)) {
            if (empty($data['name'])) {
                throw new \Exception('请输入您的姓名', self::CODE_ERROR_VIEW_TOSTA);
            }
            if (empty($data['phone'])) {
                throw new \Exception('请输入您的手机号', self::CODE_ERROR_VIEW_TOSTA);
            }
            if (empty($data['area'])) {
                throw new \Exception('请选择您所在城市', self::CODE_ERROR_VIEW_TOSTA);
            }
            $lockKey .= '-' . $this->phone;
        } else {
            $lockKey .= '-' . $this->uid;
            $userInfo = User::getBasicUserInfo($this->uid);
            $data['uid'] = $this->uid;
            $data['nick'] = $userInfo ? $userInfo['nick'] : '';
        }
        if (!BrandShowActivity::isLock($lockKey,2)) {
            $result = mNarwalDesigner::insertData($data);
            if ($result) {
                $this->setSuccess();
            } else {
                throw new \Exception('提交失败', self::CODE_ERROR_VIEW_TOSTA);
            }
        } else {
            throw new \Exception('你的操作太频繁');
        }
    }
}